# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")

set(shared_libs ${CMAKE_SOURCE_DIR}/src/main/jniLibs)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_library(libavcodec-57 SHARED IMPORTED)
set_target_properties(libavcodec-57 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libavcodec-57.so)

add_library(libavdevice-57 SHARED IMPORTED)
set_target_properties(libavdevice-57 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libavdevice-57.so)

add_library(libavfilter-6 SHARED IMPORTED)
set_target_properties(libavfilter-6 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libavfilter-6.so)

add_library(libavformat-57 SHARED IMPORTED)
set_target_properties(libavformat-57 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libavformat-57.so)

add_library(libavutil-55 SHARED IMPORTED)
set_target_properties(libavutil-55 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libavutil-55.so)

add_library(libpostproc-54 SHARED IMPORTED)
set_target_properties(libpostproc-54 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libpostproc-54.so)

add_library(libswresample-2 SHARED IMPORTED)
set_target_properties(libswresample-2 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libswresample-2.so)

add_library(libswscale-4 SHARED IMPORTED)
set_target_properties(libswscale-4 PROPERTIES
        IMPORTED_LOCATION ${shared_libs}/${ANDROID_ABI}/libswscale-4.so)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             media_jni

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/common.cpp
             src/main/cpp/jni_player.cpp
             src/main/cpp/gl_util.cpp
             src/main/cpp/gl_render.cpp
             src/main/cpp/matrix.cpp
             src/main/cpp/mediaplayer/thread.cpp
             src/main/cpp/mediaplayer/decoder_base.cpp
             src/main/cpp/mediaplayer/packetqueue.cpp
             src/main/cpp/mediaplayer/decoder_video.cpp
             src/main/cpp/mediaplayer/mediaplayer.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
       media_jni

       libavcodec-57
       libavdevice-57
       libavfilter-6
       libavformat-57
       libavutil-55
       libpostproc-54
       libswresample-2
       libswscale-4

       EGL
       GLESv2

       Threads::Threads

       # Links the target library to the log library
       # included in the NDK.
       ${log-lib} )